package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 预约信息批量上传
     *
     * @param list
     */
    @Override
    public void upload(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                long count = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    orderSettingDao.editNumber(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }

    }

    /**
     * 查询预约数据
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String before = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("before",before);
        map.put("end",end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> orderSettingList =new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String, Object> m = new HashMap<>();
            m.put("date",orderSetting.getOrderDate().getDate());
            m.put("number",orderSetting.getNumber());
            m.put("reservations",orderSetting.getReservations());
            orderSettingList.add(m);
        }
        return orderSettingList;
    }

    /**
     * 修改预约人数
     * @param orderSetting
     */
    @Override
    public void editNumber(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        long byOrderDate = orderSettingDao.findByOrderDate(orderDate);
        if (byOrderDate>0){
            orderSettingDao.editNumber(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }

    }
}
